package com.zimi.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zimi.mapper.SysDutyPrivilegeMapper;
import com.zimi.mapper.SysOrgExtendMapper;
import com.zimi.mapper.SysPostMapper;
import com.zimi.model.SysDutyPrivilege;
import com.zimi.model.SysOrgExtend;
import com.zimi.model.SysPost;
import com.zimi.model.SysUser;
import com.zimi.util.CommonUtil;
import com.zimi.util.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 职务功能授权表 Service
 *
 * @author lnintr
 * @since 2019-05-22
 */
@Service
public class SysDutyPrivilegeService {
    @Autowired
    private SysOrgExtendMapper sysOrgExtendMapper;

    @Autowired
    private SysPostMapper sysPostMapper;

    @Autowired
    private SysDutyPrivilegeMapper sysDutyPrivilegeMapper;

    @Autowired
    private CommonUtil commonUtil;

    /*@author wh
     * 取得组织和职务信息
     * */
    public Object getOrglistByDuty(SysOrgExtend sysOrgExtend) {
        // 1)权限控制
        Map map = commonUtil.getOrgList();
        List<SysOrgExtend> list = sysOrgExtendMapper.getMineOrgList(map);

        if (list != null && list.size() > 0) {
            // 2)取得指定用户的选择机构信息
            List<SysPost> listDuty = sysPostMapper.selectList(new QueryWrapper<SysPost>().lambda().eq(SysPost::getDutyId, sysOrgExtend.getDutyId()));
            Map<Integer, SysPost> dutyMap = null;

            if (listDuty != null && listDuty.size() > 0) {
                dutyMap = new HashMap<>();
                for (SysPost sore : listDuty) {
                    dutyMap.put(sore.getOrgId(), sore);
                }
            }

            // 设置选中标志的值
            for (SysOrgExtend soe : list) {
                if (dutyMap != null) {
                    SysPost sysPost = (SysPost) dutyMap.get(soe.getOrgId());

                    if (sysPost != null) {
                        soe.setId(sysPost.getId());
                        soe.setUpdateTime(sysPost.getUpdateTime());

                        if (sysPost.getPostState() == 1) {
                            soe.setChoiceFlag(true);
                        } else {
                            soe.setChoiceFlag(false);
                        }
                    } else {
                        soe.setChoiceFlag(false);
                        soe.setId(0);
                    }
                } else {
                    soe.setChoiceFlag(false);
                    soe.setId(0);
                }
            }

            // 4)取得并合并机构分类
            List<SysOrgExtend> orgCateList = sysOrgExtendMapper.getMineOgnCateList((List<Integer>) map.get("ognIdList"));

            if (orgCateList != null && orgCateList.size() > 0) {
                list.addAll(orgCateList);
            }
        }

        return list;
    }

    /*
        @author wh
         * 取得权限和职务信息
         * */
    public Object getFunctionlistByDuty(SysOrgExtend sysOrgExtend) {
        // 1)取得所有功能
        List<SysOrgExtend> list = sysOrgExtendMapper.selectFunctionList(sysOrgExtend);

        if (list != null && list.size() > 0) {
            // 2)取得所有功能信息
            List<SysDutyPrivilege> listFunction = sysDutyPrivilegeMapper.selectList(new QueryWrapper<SysDutyPrivilege>().lambda().eq(SysDutyPrivilege::getDutyId, sysOrgExtend.getDutyId()));
            Map<Integer, SysDutyPrivilege> dutyMap = null;

            if (listFunction != null && listFunction.size() > 0) {
                dutyMap = new HashMap<>();
                for (SysDutyPrivilege srpl : listFunction) {
                    dutyMap.put(srpl.getFunctionId(), srpl);
                }
            }

            // 设置选中标志的值
            for (SysOrgExtend soe : list) {
                if (dutyMap != null) {
                    SysDutyPrivilege sysDutyPrivilege = (SysDutyPrivilege) dutyMap.get(soe.getFunctionId());

                    if (sysDutyPrivilege != null) {
                        soe.setId(sysDutyPrivilege.getId());
                        soe.setUpdateTime(sysDutyPrivilege.getUpdateTime());

                        if (sysDutyPrivilege.getPrivilegeState() == 1) {
                            soe.setChoiceFlag(true);
                        } else {
                            soe.setChoiceFlag(false);
                        }
                    } else {
                        soe.setChoiceFlag(false);
                        soe.setId(0);
                    }
                } else {
                    soe.setChoiceFlag(false);
                    soe.setId(0);
                }
            }
        }

        return list;
    }

    @Transactional
    public void saveFunctionDuty(List<SysOrgExtend> sysOrgExtends, int flag) {
        List<Integer> idList = new ArrayList<>();
        if (sysOrgExtends != null && sysOrgExtends.size() > 0) {

            int dutyId = sysOrgExtends.get(0).getDutyId();
            SysDutyPrivilege tmpOrgExtend = null;

            for (SysOrgExtend soe : sysOrgExtends) {
                idList.add(soe.getFunctionId());
            }

            // 1）对要修改的对象加行锁
            List<SysDutyPrivilege> results = sysDutyPrivilegeMapper.selectList(new QueryWrapper<SysDutyPrivilege>().lambda().eq(SysDutyPrivilege::getDutyId, dutyId).in(SysDutyPrivilege::getFunctionId, idList).last("for update"));

            Map<Integer, SysDutyPrivilege> resMap = null;

            if (results != null && results.size() > 0) {
                resMap = new HashMap<>();

                for (SysDutyPrivilege sdp : results) {
                    resMap.put(sdp.getFunctionId(), sdp);
                }
            }

            SysUser user = UserUtil.getUser();
            Date updateTime = new Date();

            // 2)所属权限创建或修改
            for (SysOrgExtend soe : sysOrgExtends) {
                if (resMap != null) {
                    tmpOrgExtend = resMap.get(soe.getFunctionId());

                    // 3）判断该数据已经被改过没有
                    if (tmpOrgExtend != null) {
                        if (tmpOrgExtend.getUpdateTime() != null) {
                            if (soe.getUpdateTime() != null && soe.getUpdateTime().getTime() != tmpOrgExtend.getUpdateTime().getTime()) {
                                throw new RuntimeException("修改数据失败，数据已经被其他用户改过。");
                            }
                        }

                        tmpOrgExtend.setUpdateUser(user.getId());
                        tmpOrgExtend.setUpdateTime(updateTime);
                        tmpOrgExtend.setPrivilegeState(flag);
                        tmpOrgExtend.updateById();
                    } else {
                        tmpOrgExtend = new SysDutyPrivilege();

                        tmpOrgExtend.setCreateUser(user.getId());
                        tmpOrgExtend.setCreateTime(updateTime);
                        tmpOrgExtend.setPrivilegeState(flag);
                        tmpOrgExtend.setDutyId(dutyId);
                        tmpOrgExtend.setFunctionId(soe.getFunctionId());

                        tmpOrgExtend.insert();
                    }
                } else {
                    tmpOrgExtend = new SysDutyPrivilege();

                    tmpOrgExtend.setCreateUser(user.getId());
                    tmpOrgExtend.setCreateTime(updateTime);
                    tmpOrgExtend.setPrivilegeState(flag);
                    tmpOrgExtend.setDutyId(dutyId);
                    tmpOrgExtend.setFunctionId(soe.getFunctionId());

                    tmpOrgExtend.insert();
                }
            }
        }
    }
}
